package cgl.narada.service.security.kmc;

import java.io.Serializable;

/*
 * Some more rights for future...MAKE Appropriate changes in constructor AND add getter
 * 
 * E.g...
 * public static final int DELEGATE_RIGHT = 4;
 */
/**
 * An Object representing the rights, an entity has (subscribe OR publish OR
 * both rights) <br>
 * Created on Sep 8, 2005
 * 
 * @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
 */
public class TopicRights implements Serializable {

    public static final int PUBLISH_RIGHT = 1;

    public static final int SUBSCRIBE_RIGHT = 2;

    /**
     * Create a Topic Rights object. Once set, they cannot be modified.<br>
     * NOTE: Valid values are 0, 1, 2, 3 ONLY if any other value is specified
     * then rights are nullified (Set to 0) !
     * 
     * @param currentRights
     */
    public TopicRights(int currentRights) {
        // Check for possible errors
        if (currentRights > 3 || currentRights < 0)
            currentRights = 0;

        rights = currentRights;
    }

    private int rights;

    public String toString() {
        String ret ="";
        if(hasPublishRight())
            ret+= "[PUBLISH]";
        if(hasSubscribeRight())
            ret += "[SUBSCRIBE]";
        return ret;
    }
    
    /**
     * Check if the entity has publish rights to this topic
     * 
     * @return TRUE if the entity has publish rights, FALSE otherwise
     */
    public boolean hasPublishRight() {
        return ((rights & PUBLISH_RIGHT) == PUBLISH_RIGHT);
    }

    /**
     * Check if the entity has subscribe rights to this topic
     * 
     * @return TRUE if the entity has subscribe rights, FALSE otherwise
     */
    public boolean hasSubscribeRight() {
        return ((rights & SUBSCRIBE_RIGHT) == SUBSCRIBE_RIGHT);
    }

}
